#!/bin/bash

### 使用Miniconda3来安装python依赖
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "./miniconda-py38_4.11.0-devel.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then
    echo "请输入正确的构建路径"
    exit 1
fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then
    echo "请输入正确的安装路径"
    exit 1
fi

buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e 
yum install -y wget tar cmake environment-modules --nogpgcheck
wget -t 30 -c -P "$buildpath" https://repo.anaconda.com/miniconda/Miniconda3-py38_4.11.0-Linux-aarch64.sh

sh "$buildpath"/Miniconda3-py38_4.11.0-Linux-aarch64.sh -b -p "$installpath"/miniconda3

"$installpath"/miniconda3/condabin/conda install -y numpy scipy matplotlib setuptools
"$installpath"/miniconda3/bin/pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple mpi4py

cat >"$installpath/miniconda3/miniconda_py38_modulefiles" <<EOF
#%Module1.0
conflict miniconda_py38
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set MINICONDA_PY38 \$pwd
setenv MINICONDA_PY38 \$MINICONDA_PY38
prepend-path PATH \$MINICONDA_PY38/bin
EOF

rm -rf "$buildpath"/Miniconda3-py38_4.11.0-Linux-aarch64.sh
